fix(windows): when deleting a file use safe long windows path
authorMatthieu Gallien <matthieu.gallien@nextcloud.com>
Mon, 2 Jun 2025 08:57:57 +0000 (10:57 +0200)
committerMatthieu Gallien <matthieu.gallien@nextcloud.com>
Mon, 2 Jun 2025 11:14:15 +0000 (13:14 +0200)
will avoid getting a content access denied error because some paths is
too long for windows default limit and has to use the special syntax for
long path

see https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry

Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
src/common/filesystembase.cpp

index 41919e59f1fffab8805f53f04a46838d53237a02..38747f87079151443217d5ea841455fb9bfe50c8 100644 (file)
@@ -546,22 +546,23 @@ QString FileSystem::fileSystemForPath(const QString &path)
 
 bool FileSystem::remove(const QString &fileName, QString *errorString)
 {
+    const auto &windowsSafeFileName = FileSystem::longWinPath(fileName);
 #ifdef Q_OS_WIN
     // You cannot delete a read-only file on windows, but we want to
     // allow that.
-    setFileReadOnly(fileName, false);
+    setFileReadOnly(windowsSafeFileName, false);
 #endif
-    const auto deletedFileInfo = QFileInfo{fileName};
+    const auto deletedFileInfo = QFileInfo{windowsSafeFileName};
     if (!deletedFileInfo.exists()) {
-        qCWarning(lcFileSystem()) << fileName << "has been already deleted";
+        qCWarning(lcFileSystem()) << windowsSafeFileName << "has been already deleted";
     }
 
-    QFile f(fileName);
+    QFile f(windowsSafeFileName);
     if (!f.remove()) {
         if (errorString) {
             *errorString = f.errorString();
         }
-        qCWarning(lcFileSystem()) << f.errorString() << fileName;
+        qCWarning(lcFileSystem()) << f.errorString() << windowsSafeFileName;
 
 #if defined Q_OS_WIN
         const auto permissionsDisplayHelper = [] (std::filesystem::perms currentPermissions) {
@@ -580,10 +581,10 @@ bool FileSystem::remove(const QString &fileName, QString *errorString)
                                    << unitaryHelper(std::filesystem::perms::others_exec, 'x');
         };
 
-        const auto unsafeFilePermissions = filePermissionsWin(fileName);
+        const auto unsafeFilePermissions = filePermissionsWin(windowsSafeFileName);
         permissionsDisplayHelper(unsafeFilePermissions);
 
-        const auto safeFilePermissions = filePermissionsWinSymlinkSafe(fileName);
+        const auto safeFilePermissions = filePermissionsWinSymlinkSafe(windowsSafeFileName);
         permissionsDisplayHelper(safeFilePermissions);
 #endif